//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Input;

namespace OpenLS.Spreadsheet.UI.Internals
{
    static class CellTextEditorClipboard
    {
        public static void RegisterClassHandlers(Type controlType)
        {
            CommandHelpers.RegisterCommandHandler(controlType, ApplicationCommands.Cut,
                                                  onCut, onAlways);
            CommandHelpers.RegisterCommandHandler(controlType, ApplicationCommands.Copy,
                                          onCopy, onAlways);
            CommandHelpers.RegisterCommandHandler(controlType, ApplicationCommands.Paste,
                                          onPaste, onAlways);
        }

        private static void onCopy(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox)sender;
            Clipboard.SetText(tb.Source.SelectionText);
            e.Handled = true;
        }

        private static void onCut(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox)sender;
            Clipboard.SetText(tb.Source.SelectionText);
            tb.Source.OnDelete();
            e.Handled = true;
        }

        private static void onAlways(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
            e.Handled = true;
        }

        private static void onPaste(object sender, ExecutedRoutedEventArgs e)
        {
            var tb = (CellTextBox)sender;
            string text = Clipboard.GetText();
            tb.Source.OnTextInput(text);
            e.Handled = true;
        }
    }
}